<script>
import {removeStudents, studentPage} from "@/api/student";
import {removeUsers} from "@/api/user";


export default {
  name: "UserView",
  data() {
    return {
      size: 5,
      total: 0,
      currentPage: 1,
      selectionIds: [],
      tableData: [
        //   一个对象 就表示表格中的一行数据   一条记录
      ]
    }
  },
  // 单击事件的回调函数
  methods: {
    // 每页展示条数改变时触发
    handleSizeChange(val) {
      this.size = val
      this.initStudentPage()
    },
    // 改变页码时自动触发
    handleCurrentChange(val) {
      this.currentPage = val
      this.initStudentPage()
    },
    // 编辑数据的回调函数
    handleEdit(index, row) {
      console.log("编辑用户信息");
    },
    // 删除数据的回调函数
    handleDelete(index, row) {
      // 用户的确认框
      this.$confirm('此操作将永久删除行数据, 是否继续?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        removeStudents([row.id]).then(response=>{
          if (response.code == 200) {
            this.$message.success(response.message)
            this.initStudentPage()
          }else{
            this.$message.error(response.message)
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    // 复选框回调函数
    handleSelectionChange(val) {
      this.selectionIds = val.map(v => v.id)
      console.log(JSON.stringify(this.selectionIds))
    },
    initStudentPage() {
      studentPage(this.currentPage, this.size).then(response => {
        if (response.code == 200) {
          this.tableData = response.data
          this.total = response.count
        }
      })
    },
    deleteUsers() {
      if (this.selectionIds.length == 0) {
        this.$message.warning('请至少选择一条数据')
        return false
      }
      // 发送http请求
      removeStudents(this.selectionIds).then(response => {
        if (response.code == 200) {
          this.$message.success(response.message)
          this.initStudentPage()
        } else {
          this.$message.error(response.message)
        }
      })
    }
  },
  created() {
    this.initStudentPage()
  }
}
</script>

<template>
  <!--组件结构代码编写的位置-->
  <div id="container">
    <div id="btn-header">
      <el-button type="primary" plain size="small">新增</el-button>
      <el-button type="success" plain size="small" @click="deleteUsers">批量删除</el-button>
    </div>
    <el-table
        stripe
        :data="tableData"
        @selection-change="handleSelectionChange"
        style="width: 100%">
      <el-table-column
          type="selection"
          width="55">
      </el-table-column>
      <el-table-column
          prop="stuNo"
          label="学号">
      </el-table-column>
      <el-table-column
          prop="stuName"
          label="姓名">
      </el-table-column>
      <el-table-column
          prop="sex"
          label="性别">
      </el-table-column>
      <el-table-column
          prop="age"
          label="年龄">
      </el-table-column>
      <el-table-column
          prop="clazz"
          label="班级">
      </el-table-column>
      <el-table-column
          prop="tel"
          label="联系方式">
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
              size="mini"
              @click="handleEdit(scope.$index, scope.row)">编辑
          </el-button>
          <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <div id="pagination">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="size"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </div>
  </div>
</template>

<style scoped>
#btn-header {
  /*调整按钮的位置*/
  text-align: left;
  margin-bottom: 10px;
  margin-top: 15px;
}

#container {
  padding: 20px;
}

#pagination {
  margin-top: 15px;
}
</style>